package com.wondertek.service.sdk.iotdh.config;

import cn.hutool.core.collection.CollectionUtil;
import com.wondertek.common.exception.AppException;
import com.wondertek.common.util.base.DateUtil;
import com.wondertek.common.util.base.StringUtil;
import com.wondertek.service.sdk.iotdh.dto.req.SubscribeDatasDhReq;
import com.wondertek.service.sdk.iotdh.service.DhDeviceService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletContext;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Dh数据订阅
 * 先判断有无订阅，没有订阅则重新订阅一次
 */
//@Component
//@ConfigurationProperties(prefix = "subscribe")
//@Data
public class MyCommandLineRunner implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(MyCommandLineRunner.class);

    private static final String postfix = "/iot-web-test/dh/message/callBackUrl/receiveMessages";

    private List<Integer> typeList;

    private String callBackUrl;


    @Resource
    private DhDeviceService dhDeviceService;

    @Resource
    private ServletContext servletContext;

    @Override
    public void run(String... args) {
        if (CollectionUtil.isEmpty(typeList)) {
            throw new AppException("springboot启动告警回调订阅失败:typeList为空!");
        }
        if (StringUtil.isBlank(callBackUrl)) {
            throw new AppException("springboot启动告警回调订阅失败:callBackUrl为空!");
        }

        try {
            SubscribeDatasDhReq subscribeData = new SubscribeDatasDhReq();
            String url = callBackUrl.concat(postfix);
            subscribeData.setSubscribeType(typeList);
            subscribeData.setCallBackUrl(url);
            dhDeviceService.subscribeDatas(subscribeData);
            log.info("订阅成功->{},typeList->{},url->{}", DateUtil.date2LongString(new Date()), typeList, url);
        } catch (Exception e) {
            log.info("springboot启动告警回调订阅失败:sdk接口调用失败!");
        }
    }
}